import QtQuick 1.0
import com.nokia.meego 1.0
import com.nokia.extras 1.0

Page {

    function insertChar(character){
        var mac = txtOui.text;

        if(mac.length>4){
            if (mac.length<8){
                txtOui.text=mac+character;
            }
        }else if (mac.length>2){
            txtOui.text=mac+character;
            if (txtOui.text.length==5){
                txtOui.text=txtOui.text+":";
            }
        }else{
            txtOui.text=mac+character;
            if (txtOui.text.length==2){
                txtOui.text=txtOui.text+":";
            }
        }
    }

    function deleteLast(){
        if (txtOui.text.length>0){
            if (txtOui.text.charAt(txtOui.text.length-1)==':'){
                txtOui.text=txtOui.text.substring(0,txtOui.text.length-2);
            }else{
                txtOui.text=txtOui.text.substring(0,txtOui.text.length-1);
            }
        }
    }

    InfoBanner{
        id: infoBanner;
        timerEnabled: true
        timerShowTime: 2500
        z: 1
    }

    /*void WMain::insertChar(QString character){
        QString mac = edtOUI->text();
        if (mac.count()>6){
        edtOUI->setText(mac.insert(mac.count(),character));
        }else if (mac.count()>4){
        edtOUI->setText(mac.insert(mac.count()-1,character));
        }else{
        edtOUI->setText(mac.insert(mac.count()-2,character));
        }
        m_context->setContextProperty("currentOUI", QString(edtOUI->text()));
    }*/

    Label {
        id: lblOui

        anchors.left: parent.left
        anchors.leftMargin: 5
        anchors.verticalCenter: txtOui.verticalCenter

        text: "OUI:"
    }

    TextArea{
        id: txtOui

        anchors.top: parent.top
        anchors.topMargin: 5
        anchors.left: lblOui.right
        anchors.leftMargin: 5

        placeholderText: "__:__:__"
        enableSoftwareInputPanel : false
    }

    Button{
        id: btnHelp

        anchors.top: parent.top
        anchors.topMargin: 5
        anchors.left: txtOui.right
        anchors.leftMargin: 5
        anchors.right: parent.right
        anchors.rightMargin: 5

        height: txtOui.height

        text: "?";

        platformStyle: ButtonStyle

        onClicked: {
            infoBanner.text="Fill in the first six hexadecimal characters of a MAC address";
            infoBanner.show();
        }
    }

    Label {
        id: lblOrg

        anchors.left: parent.left
        anchors.leftMargin: 5
        anchors.verticalCenter: txtOrg.verticalCenter

        text: "Org:"
    }

    TextArea{
        id: txtOrg

        anchors.top: txtOui.bottom
        anchors.topMargin: 10
        anchors.left: lblOrg.right
        anchors.leftMargin: 5
        anchors.right: parent.right
        anchors.rightMargin: 5

        enableSoftwareInputPanel : false
    }

    Flow {
        id: flow

        anchors.top: txtOrg.bottom
        anchors.topMargin: 10
        anchors.bottom: parent.bottom
        anchors.left: parent.left
        anchors.leftMargin: 2
        anchors.right: parent.right
        anchors.rightMargin: 2

        spacing: 2

        ButtonStyle {
            id: btnStyle
            pressedBackground: "image://theme/color2-meegotouch-button-accent-background"
        }

        Button {
            id: btn0;

            text: "0";
            width: 92;
            height: 92;

            onClicked:{
                insertChar(text);
            }

            platformStyle: btnStyle
        }

        Button {
            id: btn1;

            text: "1";
            width: 92;
            height: 92;

            onClicked:{
                insertChar(text);
            }

            platformStyle: btnStyle
        }

        Button {
            id: btn2;

            text: "2";
            width: 92;
            height: 92;

            onClicked:{
                insertChar(text);
            }

            platformStyle: btnStyle
        }

        Button {
            id: btn3;

            text: "3";
            width: 92;
            height: 92;

            onClicked:{
                insertChar(text);
            }

            platformStyle: btnStyle
        }

        Button {
            id: btn4;

            text: "4";
            width: 92;
            height: 92;

            onClicked:{
                insertChar(text);
            }

            platformStyle: btnStyle
        }

        Button {
            id: btn5;

            text: "5";
            width: 92;
            height: 92;

            onClicked:{
                insertChar(text);
            }

            platformStyle: btnStyle
        }

        Button {
            id: btn6;

            text: "6";
            width: 92;
            height: 92;

            onClicked:{
                insertChar(text);
            }

            platformStyle: btnStyle
        }

        Button {
            id: btn7;

            text: "7";
            width: 92;
            height: 92;

            onClicked:{
                insertChar(text);
            }

            platformStyle: btnStyle
        }

        Button {
            id: btn8;

            text: "8";
            width: 92;
            height: 92;

            onClicked:{
                insertChar(text);
            }

            platformStyle: btnStyle
        }

        Button {
            id: btn9;

            text: "9";
            width: 92;
            height: 92;

            onClicked:{
                insertChar(text);
            }

            platformStyle: btnStyle
        }

        Button {
            id: btnA;

            text: "A";
            width: 92;
            height: 92;

            onClicked:{
                insertChar(text);
            }

            platformStyle: btnStyle
        }

        Button {
            id: btnB;

            text: "B";
            width: 92;
            height: 92;

            onClicked:{
                insertChar(text);
            }

            platformStyle: btnStyle
        }

        Button {
            id: btnC;

            text: "C";
            width: 92;
            height: 92;

            onClicked:{
                insertChar(text);
            }

            platformStyle: btnStyle
        }

        Button {
            id: btnD;

            text: "D";
            width: 92;
            height: 92;

            onClicked:{
                insertChar(text);
            }

            platformStyle: btnStyle
        }

        Button {
            id: btnE;

            text: "E";
            width: 92;
            height: 92;

            onClicked:{
                insertChar(text);
            }

            platformStyle: btnStyle
        }
        Button {
            id: btnF;

            text: "F";
            width: 92;
            height: 92;

            onClicked:{
                insertChar(text);
            }

            platformStyle: btnStyle
        }

        Button {
            id: btnBack;
            width: 92;
            height: 92;
            onClicked: {
                deleteLast();
            }

            Image {
                source: "qrc:/resources/icons/left.png"
                anchors.verticalCenter: parent.verticalCenter
                anchors.horizontalCenter: parent.horizontalCenter
            }

            platformStyle: btnStyle
        }

        Button {
            id: btnClear;
            text: "Clear";
            width: 92;
            height: 92;
            onClicked: {
                txtOui.text="";
                txtOrg.text="";
            }

            platformStyle: btnStyle
        }

        Button {
            id: btGetOrg;
            text: "Get Org";
            width: 92*2;
            height: 92;
            onClicked: {
                txtOrg.text="";
                if (txtOui.text.length>7){
                    txtOrg.text=wMain.getOrg(txtOui.text);
                }else{
                    infoBanner.text="Please fill the OUI first";
                    infoBanner.show();
                }
            }
        }
    }
}
